package com.lagou.service.impl;


import com.lagou.Constant.Constant;
import com.lagou.service.ServiceRegistry;
import org.I0Itec.zkclient.ZkClient;

public class ZkServiceRegistry implements ServiceRegistry {

    private String zkAddress = "192.168.193.132:2181";

    private ZkClient zkClient;

    public void init() {

        zkClient = new ZkClient(zkAddress,
                60000,
                6000);
        System.out.println(">>> connect to zookeeper");

    }

    @Override
    public void registry(String serviceAddress) {

        //创建registry节点（持久）
        String registryPath = Constant.ZK_REGISTRY;
        if (!zkClient.exists(registryPath)) {
            zkClient.createPersistent(registryPath);
            System.out.println(">>> create registry node:" + registryPath);
        }

//        //创建service节点（持久）
//        String servicePath = registryPath + "/" + serviceName;
//        if (!zkClient.exists(servicePath)) {
//            zkClient.createPersistent(servicePath);
//            System.out.println(">>>create service node:" + servicePath);
//        }

        //创建address节点（临时）
        String addressPath =  registryPath+"/address-"+serviceAddress;
        String addressNode = zkClient.createEphemeralSequential(addressPath,serviceAddress);
        System.out.println(">>> create address node:" + addressNode);

    }

}